﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using StockAnalysisModel;
using System.Threading;
using StockAnalysisModel;
using Wusong.Dal.EF;
using System.Data.Objects;

namespace QQStockInfoReader
{
    public partial class ReaderForm : Form
    {
        AreaStockReader reader;
        System.Threading.Timer timer;
        private int _areaCount = 9;
        private int activedCount = 1;
        public ReaderForm()
        {
            InitializeComponent();

            reader = new AreaStockReader(this.webBrowser1);
            this.webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(OnBrowserDocumentCompleted);
            this.webBrowser1.Navigate(reader._formatStockAreaUrl);
        }


        private void OnBrowserDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //reader.Read();
            //while (!reader.IsComplete)
            //{

            //}
            //List<StockDailyInfo> infos = reader.Result;
        }

        private void btnInvokeAreaTab_Click(object sender, EventArgs e)
        {
            reader.InvokeAreaTabClickEventByHand();
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            reader.Read();
            List<StockDailyInfo> infos = reader.Result;
            using (ObjectContext dbContext = new StockAnalysisModel.StockAnalysisEntities())
            {
                try
                {
                    Wusong.Dal.Inf.IObjDAO<StockDailyInfo> dao = new EF4DAO<StockDailyInfo>(dbContext);
                    foreach (StockDailyInfo item in infos)
                    {
                        dao.AddEntity(item, false);

                    }
                    dbContext.SaveChanges();
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                }
            }
            MessageBox.Show("读取完毕");
            this.btnRead.Visible = false;
        }

        private void btnAnalysis_Click(object sender, EventArgs e)
        {
            AreaAnalysisMainForm analysisFrm = new AreaAnalysisMainForm();
            analysisFrm.Show();
        }
    }
}
